package com.lanou.utils;

import java.sql.*;
import java.util.ResourceBundle;

/**
 * Created by dllo on 18/1/19.
 */
public class JDBCUtils {
    private static String driver;
    private static String url;
    private static String user;
    private static String psw;

    static {
        ResourceBundle rb = ResourceBundle.getBundle("db");
        driver = rb.getString("driverName");
        url = rb.getString("url");
        user = rb.getString("userName");
        psw = rb.getString("password");

        try {
            Class.forName(driver);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url,user,psw);
    }
    public static void close( ResultSet rs,PreparedStatement pstm , Connection connection){
        if (rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }if (pstm!=null){
            try {
                pstm.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }if (connection !=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(PreparedStatement pst , Connection c){
        if (pst!=null){
            try {
                pst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }if (c != null){
            try {
                c.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


}
